package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io"
	"os"
	"unicode"
)

// bytes包中Contains函数测试
func bytesContains() {
	fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
	fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
	fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
	fmt.Println(bytes.Contains([]byte(""), []byte("")))
}

func bytesCounts(){
	fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
	fmt.Println(bytes.Count([]byte("five"), []byte("")))
}

func bytesEqualFold() {
	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
}

func bytesFields() {
	fmt.Printf("Fields are: %q",bytes.Fields([]byte(" foo bar  	baz  		")))
}

func bytesFieldsFunc() {
	f := func(c rune) bool {
		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
		//return unicode.IsLetter(c)
	}

	fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte("   foo1;bar,baz3..."), f))
}

func bytesHasPrefix() {
	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("c")))
	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
}

func bytesHasSuffix() {
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("o")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
}

func bytesIndex() {
	fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
	fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
}

func bytesIndexAny() {
	fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
	fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
}

func bytesIndexFunc() {
	f := func(c rune) bool {
		return unicode.Is(unicode.Han, c)
	}

	fmt.Println(bytes.IndexFunc([]byte("hello, 世界"), f))
	fmt.Println(bytes.IndexFunc([]byte("Hello, World"), f))
}

func bytesIndexRune() {
	fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
	fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
}

func bytesJoin() {
	s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
	fmt.Printf("%s", bytes.Join(s, []byte(", ")))
}

func bytesLastIndex() {
	fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
}

func bytesMap() {
	rot13 := func(r rune) rune{
		switch {
		case r >= 'A' && r <= 'Z':
			return 'A' + (r - 'A' + 13) % 26
		case r >= 'a' && r <= 'z':
			return 'a' + (r - 'a' + 13) % 26
		}
		return r
	}

	fmt.Printf("%s", bytes.Map(rot13, []byte("Twas brilling and the slithy gother...")))
}

func bytesRepeat() {
	fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
}

func bytesReplace() {
	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
}

func bytesSplit() {
	fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
	fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
	fmt.Printf("%q\n", bytes.Split([]byte(" xyz"), []byte("")))
	fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
}

func bytesSplitAfter() {
	fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
	fmt.Printf("%q\n", bytes.SplitAfter([]byte("a man a plan a canal panama"), []byte("a ")))
}

func bytesSplitAfterN() {
	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 0))
	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), -1))
}

func bytesSplitN() {
	fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
	fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 0))
	fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), -1))
	z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
	fmt.Printf("%q (nil = %v)\n", z, z == nil)
}

func bytesTitle() {
	fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
}

func bytesTolLower(){
	fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
}

func bytesToTitle() {
	fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
	fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
}

func bytesToUpper()  {
	fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
}

func bytesTrim() {
	fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
}

func bytesBuffer() {
	var b bytes.Buffer
	b.Write([]byte("Hello "))
	fmt.Fprintf(&b, "world!")
	b.WriteTo(os.Stdout)
}

func bytesBufferReader() {
	buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
	dec := base64.NewDecoder(base64.StdEncoding, buf)
	io.Copy(os.Stdout, dec)
}

func main() {
	//bytesContains()
	//bytesCounts()
	//bytesEqualFold()
	//bytesFields()
	//bytesFieldsFunc()
	//bytesHasPrefix()
	//bytesHasSuffix()
	//bytesIndex()
	//bytesIndexAny()
	//bytesIndexFunc()
	//bytesIndexRune()
	//bytesJoin()
	//bytesLastIndex()
	//bytesMap()
	//bytesRepeat()
	//bytesReplace()
	//bytesSplit()
	//bytesSplitAfter()
	//bytesSplitAfterN()
	//bytesSplitN()
	//bytesTitle()
	//bytesTolLower()
	//bytesToTitle()
	//bytesToUpper()
	//bytesTrim()
	//bytesBuffer()
	bytesBufferReader()
}



